package com.rayboot.footpoint.commonui;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import com.rayboot.footpoint.model.Footprint;

public class MapPhotoPinDrawable extends Drawable {
	private static Drawable mNormalDrawable = null;
	private static Drawable mSelDrawable = null;
	private Drawable mPhoto;
	protected boolean mSelected;
	private boolean isTransparent;

	private Footprint footprint;

	public MapPhotoPinDrawable(Footprint footprint, Drawable normal,
			Drawable sel, boolean transparent) {
		this.footprint = footprint;
		if (mNormalDrawable == null) {
			mNormalDrawable = normal;
		}
		if (mSelDrawable == null) {
			mSelDrawable = sel;
		}
		Rect rect = mNormalDrawable.copyBounds();
		setBounds(rect);
		if (!TextUtils.isEmpty(footprint.mPicPath)) {
			rect.left = (11 + rect.left);
			rect.right -= 14;
			rect.top = (11 + rect.top);
			rect.bottom -= 45;
			this.mPhoto = new BitmapDrawable(footprint.mPicPath);
			this.mPhoto.setBounds(rect);
		}
		this.isTransparent = transparent;
	}

	public MapPhotoPinDrawable(Drawable photo, Drawable normal, Drawable sel,
			boolean transparent) {

		this.mPhoto = photo;
		if (mNormalDrawable == null) {
			mNormalDrawable = normal;
		}
		if (mSelDrawable == null) {
			mSelDrawable = sel;
		}
		Rect rect = mNormalDrawable.copyBounds();
		setBounds(rect);
		if (this.mPhoto != null) {
			rect.left = (11 + rect.left);
			rect.right -= 14;
			rect.top = (11 + rect.top);
			rect.bottom -= 45;
			this.mPhoto.setBounds(rect);
		}
		this.isTransparent = false;
	}

	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		Rect rectNormal = mNormalDrawable.getBounds();
		if (mPhoto != null) {
			Rect rectPhoto = mPhoto.getBounds();
			if (this.isTransparent) {
				canvas.clipRect(rectPhoto.left, rectPhoto.top, rectPhoto.right,
						rectPhoto.bottom, Region.Op.DIFFERENCE);
			}
		}
		canvas.clipRect(rectNormal);
		if (mSelected) {
			mSelDrawable.getCurrent().draw(canvas);
		} else {
			mNormalDrawable.getCurrent().draw(canvas);
		}
		if (mPhoto != null) {
			mPhoto.draw(canvas);
		}

	}

	@Override
	public int getOpacity() {
		// TODO Auto-generated method stub
		if (mPhoto != null) {
			return mPhoto.getOpacity();
		}
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
		// TODO Auto-generated method stub
		if (mPhoto != null) {
			mPhoto.setAlpha(alpha);
		}
		mNormalDrawable.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		// TODO Auto-generated method stub
		if (mPhoto != null) {
			mPhoto.setColorFilter(cf);
		}
		mNormalDrawable.setColorFilter(cf);
		mSelDrawable.setColorFilter(cf);
	}

	@Override
	public boolean setState(int[] stateSet) {
		// TODO Auto-generated method stub
		mNormalDrawable.setState(stateSet);
		mSelDrawable.setState(stateSet);
		if (this.mPhoto != null)
			this.mPhoto.setState(stateSet);
		mSelected = false;
		for (int i : stateSet) {
			if (i == android.R.attr.state_pressed) {
				mSelected = true;
			}
		}
		return super.setState(stateSet);
	}

}
